<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-08-16
 * Time: 16:38
 */

namespace plugin\csauto\app\controller;

use plugin\csauto\app\common\BaseController;
use plugin\csauto\app\model\RssModel;
use plugin\csauto\app\model\SiteModel;
use plugin\csauto\app\service\ApiWorkService;
use plugin\csauto\app\service\SiteRulesWorkService;
use plugin\csauto\app\service\SiteWorkService;
use support\Request;
use support\Response;

class ApiController extends BaseController
{


    public function get(Request $request): Response
    {
        $List = array();
        switch ($request->input('name', null)) {
            case 'rss_site':
                $siteModel = SiteModel::where('get_status', 1)->get();
                foreach ($siteModel as $item) {
                    $List[] = array(
                        'value' => $item->id,
                        'name' => $item->site_name
                    );
                }
                break;
            case 'site':
                $siteModel = SiteModel::get();
                foreach ($siteModel as $item) {
                    $List[] = array(
                        'value' => $item->id,
                        'name' => $item->site_name
                    );
                }
                break;
            case 'site_out':
                $siteModel = SiteModel::where('out_status', 1)->get();
                foreach ($siteModel as $item) {
                    $List[] = array(
                        'value' => $item->id,
                        'name' => $item->site_name
                    );
                }
                break;
            case 'rule_type':
                foreach (SiteRulesWorkService::$RuleTypeENUM as $key => $value) {
                    $List[] = array(
                        'value' => $key,
                        'name' => $value
                    );
                }
                break;
            case 'rule_compare':
                foreach (SiteRulesWorkService::$RuleFormulaENUM as $key => $value) {
                    $List[] = array(
                        'value' => $key,
                        'name' => $value
                    );
                }
                break;
            case 'rule_action':
                foreach (SiteRulesWorkService::$RuleActionENUM as $key => $value) {
                    $List[] = array(
                        'value' => $key,
                        'name' => $value
                    );
                }
                break;
            case 'task_status':
                $taskStatusMap = [
                    0 => '待发布',
                    1 => '发布成功',
                    2 => '资源重复',
                    3 => '发布失败',
                    4 => '任务不存在',
                    5 => '任务已结束',
                    6 => '源站种子不存在',
                    99 => '完结'
                ];
                foreach ($taskStatusMap as $key => $value) {
                    $List[] = array(
                        'value' => $key,
                        'name' => $value
                    );
                }
                break;
            case 'download_status':
                $downloadStatusMap = [
                    0 => '待下载',
                    1 => '下载中',
                    2 => '下载完成'
                ];
                foreach ($downloadStatusMap as $key => $value) {
                    $List[] = array(
                        'value' => $key,
                        'name' => $value
                    );
                }
                break;
            case 'mediainfo_status':
                $mediainfoStatusMap = [
                    0 => '未获取',
                    2 => '获取完成'
                ];
                foreach ($mediainfoStatusMap as $key => $value) {
                    $List[] = array(
                        'value' => $key,
                        'name' => $value
                    );
                }
                break;
        }

        return $this->json(0, 'ok', $List);
    }

    public function updateSite(Request $request): Response
    {
        $apiWorkData = ApiWorkService::SiteList();
        if (!$apiWorkData) return $this->fail('更新失败');
        SiteWorkService::updateSite($apiWorkData);
        return $this->json(0, 'ok');
    }


}